<template>
  <el-container class="container">
    <!-- 头部区域 -->
    <el-header>
      <div class="left">
        <!-- <img class="logo_top" src="..\assets\Images\login\logo.png" /> -->
        <span>聚图信息系统</span>
      </div>
      <!-- 用户登录展示的头像 -->
      <div>
        <div class="name">欢迎您!亲爱的{{ manager_name }}</div>
        <el-dropdown>
          <img src="../assets/Images/manager/user_pic.png" class="user_img" />
          <!-- 用户下拉菜单 -->
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item icon="el-icon-house">
              <div @click="gohome" class="gohome">系统首页</div>
            </el-dropdown-item>
            <el-dropdown-item icon="el-icon-switch-button">
              <div @click="logout" class="gohome">退出登录</div>
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
      <!-- <el-button type="info" @click="logout">退出</el-button> -->
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="200px">
        <div class="toggle-button">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu background-color="#4c61a0" text-color="#fff" active-text-color="#ffd04b" :unique-opened="true"
          :router="true" :default-active="activePath">
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
            <template slot="title">
              <i :class="item.iconName"></i>
              <span>{{ item.permissionName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="subItem.ppath" v-for="subItem in item.children" :key="subItem.id"
              @click="saveNavState(subItem.ppath)">
              <template slot="title">
                <i :class="subItem.iconName"></i>
                <span>{{ subItem.permissionName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Manager',
  data () {
    return {
      manager_name: '',
      // 左侧菜单数据
      menulist: [],
      // 被激活的链接地址
      activePath: ''
    }
  },
  created () {
    this.getMenuList()
    this.getName()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 获取所有的菜单
    async getMenuList () {
      const { data: res } = await this.$axios.get('admin/menu')
      if (res.code !== 200) {
        return this.$message.error(res.message)
      }
      this.menulist = res.data
    },
    // 获取管理员类型名
    async getName () {
      const { data: res } = await this.$axios.get('login/getrole')
      this.manager_name = res.data.userrole
    },
    // 保存链接的激活状态
    saveNavState (path) {
      window.sessionStorage.setItem('activePath', path)
      this.activePath = path
    },
    // 退出登录
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 回到首页
    gohome () {
      // window.sessionStorage.clear()
      this.$router.replace('/')
    }
  }
}
</script>

<style scoped>
.container {
  height: 100vh;
}

.el-header {
  background-color: #7489c7;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #dda455;
  font-size: 20px;
}

/* 用户登录头像 */
.user_img {
  margin-right: 10px;
  margin-left: 10px;
  margin-top: 6px;
  width: 50px;
  border-radius: 50%;
}

/* 下拉菜单 */
.el-dropdown-link {
  cursor: pointer;
  color: #409EFF;
}

.name {
  float: left;
  margin-top: 20px;
}

.gohome {
  display: inline;
  text-decoration: none;
  color: gray;
  padding: 10px;
}

.el-aside {
  background-color: #4c61a0;
}

.toggle-button {
  background-color: #515f8a;
  font-size: 14px;
  line-height: 30px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}

.el-menu {
  border-right: none;
}

.el-main {
  background-color: #e8edf0;
}
</style>
